package ferp.android.views;

import android.content.Context;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import ferp.android.GUI;
import ferp.android.R;

/**
* User: user
* Date: 6/22/12 7:40 PM
*/
class TrickCounterView implements ViewSwitcher.ViewFactory
{
  TrickCounterView(Context context, ViewGroup parent)
  {
    this.context = context;

    switcher = new TextSwitcher(context);

    switcher.setFactory(this);
    switcher.setBackgroundResource(R.drawable.trick_counter_bg);
    switcher.setForeground(context.getResources().getDrawable(R.drawable.trick_counter_fg));

    reset();

    parent.addView(switcher);
  }

  public void reset() {set(0);}
  public void set(int count) {switcher.setCurrentText(String.valueOf(count));}
  public void update(int count) {switcher.setText(String.valueOf(count));}

  public void layout(Rect where)
  {
    switcher.measure(View.MeasureSpec.EXACTLY | where.width(), View.MeasureSpec.EXACTLY | where.height());

    // scale the text in text switcher according to it's height
    int ts = Math.round(where.height() / 2.7f);

    ((TextView)switcher.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_PX, ts);
    ((TextView)switcher.getChildAt(1)).setTextSize(TypedValue.COMPLEX_UNIT_PX, ts);

    switcher.layout(where.left, where.top, where.right, where.bottom);
  }

  // ViewFactory implementation
  public View makeView()
  {
    TextView tv = new TextView(context);
    FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, Gravity.CENTER);

    tv.setLayoutParams(flp);
    tv.setGravity(Gravity.CENTER);
    tv.setTypeface(GUI.Font.BOLD);
    tv.setTextColor(GUI.Color.NORMAL);
    tv.setText("", TextView.BufferType.SPANNABLE);

    return tv;
  }

  private final Context context;
  private final TextSwitcher switcher;
}
